import { _decorator, Component, Sprite, UITransform } from "cc";
import { PlayerStateMachine } from "../Script/Player/PlayerStateMachine";
import { DIRECTION_ENUM, DIRECTION_ORDER_ENUM, ENTITY_STATE_ENUM, ENTITY_TYPE_ENUM, PARAMS_NAME_ENUM } from "../Enum";
import { TILE_HEIGHT, TILE_WIDTH } from "../Script/Tile/TileManager";
import { IEntity } from "../levels";



const { ccclass, property } = _decorator;

const ANIMATION_SPEED = 1 / 8;

@ccclass("EntityManger")
export class EnityManager extends Component {

   x: number;
   y: number;
   fsm: PlayerStateMachine
   private _direction: DIRECTION_ENUM;
   private _state: ENTITY_STATE_ENUM;
   type: ENTITY_TYPE_ENUM;

   get direction() {
      return this._direction;
   }

   set direction(newDirection: DIRECTION_ENUM) {
      this._direction = newDirection;
      this.fsm.setParams(PARAMS_NAME_ENUM.DIRECTION, DIRECTION_ORDER_ENUM[this._direction])
   }

   get state() {
      return this._state;
   }

   set state(newState: ENTITY_STATE_ENUM) {
      this._state = newState;
      this.fsm.setParams(newState, true)
   }

   init(params: IEntity) {
      console.log("我被执行了-2")
      console.log("paramsx : ",params.x)
      console.log("paramsy : ",params.y)
      const sprite = this.node.addComponent(Sprite);
      sprite.sizeMode = Sprite.SizeMode.CUSTOM
      const transFrom = this.getComponent(UITransform);
      transFrom.setContentSize(TILE_WIDTH * 4, TILE_HEIGHT * 4);
      this.x = params.x;
      this.y = params.y;
      this.type = params.type;
      this.direction = params.direction;
      this.state = params.state;
   }

   update() {
      this.node.setPosition(this.x * TILE_WIDTH - TILE_WIDTH * 1.5, -this.y * TILE_HEIGHT + TILE_HEIGHT * 1.5)
   }

}


